<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>阿里巴巴集团杯ACM-ICPC程序设计精英赛-网络赛赛前登记</title>
	<?php require_once('./include/db_info.inc.php'); ?>
</head>
<body>
<center><h1>阿里巴巴集团杯ACM-ICPC程序设计精英赛-网络赛赛前登记</h1></center>
<?php
	if (isset($_POST['sid'])) {?>
		<center>
		<h3>校验中。。。</h3>
		<?php 
		if (isset($_SESSION['user_id']) && $_SESSION['user_id'] == $_POST['sid']){
			$ojid = $_POST['sid'];
			$name = $_POST['name'];
			$stdnum = $_POST['stdnum'];
			$id = $_POST['pid']; if (strlen($id)==18 && $id[17]=='x') $id[17] = 'X';
			$school = $_POST['ishust'];
			$email = $_POST['email'];
			$flag = false;
			if (strpos($ojid, "'")!=false || strpos($name, "'")!=false || strpos($stdnum, "'")!=false || strpos($id, "'")!=false || strpos($school, "'")!=false || strpos($email, "'")!=false) {
				echo "<h5>单引号错误，请不要输入单引号。</h5>";
				$flag = true;
			} else {
			//================= OJ ID CHECK ===================
			$sql = "SELECT count(*) FROM `alibaba` WHERE `ojid`='".$_POST['sid']."'";
			$result = mysql_query($sql) or die(mysql_error());
			$row = mysql_fetch_array($result);
			$retcount = intval($row[0]);
			if ($retcount != 0) { echo "<h5>本ID已经登记过信息无需再次登记</h5>"; $flag = true; }
			//================= STD NUMBER CHECK ==============
			if (strlen($_POST['stdnum']) > 40) { $flag = true; echo "<h5>学号信息过长。。。</h5>"; }
			if (isset($_POST['stdnum']) && $_POST['stdnum']!=null){
			if ($_POST['ishust'] == 1){
			$sql = "SELECT count(*) FROM `alibaba` WHERE `stdnum`='".$_POST['stdnum']."'";
			$result = mysql_query($sql) or die(mysql_error());
			$row = mysql_fetch_array($result);
			$retcount = intval($row[0]);
			if ($retcount != 0) { echo "<h5>本学生证号已经使用过不可再次使用</h5>"; $flag = true; }
			}
			} else {
				echo "<h5>您还没有填写学生证号</h5>";
				$flag = true;
			}
			//================= Personal ID CHECK =============
			if (isset($_POST['pid']) && $_POST['pid']!=null){
			$sql = "SELECT count(*) FROM `alibaba` WHERE `id`='".$_POST['pid']."'";
			$result = mysql_query($sql) or die(mysql_error());
			$row = mysql_fetch_array($result);
			$retcount = intval($row[0]);
			if ($retcount != 0) { echo "<h5>本身份证号已经被使用过，不可再次使用</h5>"; $flag = true; }
			if (!(strlen($id)==18 && (is_numeric($id) || (is_numeric(substr($id, 0, 17)) && $id[17]=='X')))) { echo "<h5>身份证号必须是18位数字</h5>";  $flag = true; }
			else {
				$w = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
				$s = 0;
				for ($i=0; $i<18; ++$i) 
					if ($id[$i]!='X') $s += $w[$i] * $id[$i]; else $s += $w[$i] * 10;
				$r = $s % 11;
				$c = array(1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2);
				if ($c[$r] != $_POST['pid'][17]) {
					echo "<h5>本身份证是无效身份证，请输入正确的身份证号码。</h5>";
					$flag = true;
				}
			}
			} else {
				echo "<h5>您还没有填写身份证号码。</h5>";
				$flag = true;
			}
			if (isset($_POST['email']) && $_POST['email']!=null){
			if (strlen($_POST['email']) > 100) { $flag = true; echo "<h5>Email信息过长。。。</h5>"; }
			$sql = "SELECT count(*) FROM `alibaba` WHERE `email`='".$_POST['email']."'";
			$result = mysql_query($sql) or die(mysql_error());
			$row = mysql_fetch_array($result);
			$retcount = intval($row[0]);
			if ($retcount != 0) { echo "<h5>本邮箱已经被注册过不可再次注册</h5>"; $flag = true; }
			} else {
				echo "<h5>您还没有填写邮箱</h5>";
				$flag = true;
			}
			if (isset($_POST['name']) && $_POST['name']!=null){
				if (strlen($_POST['name']) > 20) { $flag = true; echo "<h5>姓名过长。。。</h5>"; }
			} else {
				echo "<h5>您还没有填写姓名</h5>";
				$flag = true;
			}
			}
			if ($flag) echo "<h5>登记失败，请核查以上几项错误信息后再次提交。</h5>";
			else {
				$sql = "INSERT INTO `alibaba`(`ojid`,`name`,`stdnum`,`id`,`school`,`email`)values('$ojid','$name','$stdnum','$id','$school','$email')";
				mysql_query($sql);
				$sql = "INSERT INTO `privilege`(`user_id`,`rightstr`)values('$ojid','c1079')";
				mysql_query($sql);
				echo "<h5>恭喜登记成功，点<a href=\"http://acm.hust.edu.cn/thx/contest.php?cid=1079\">这里</a>进入比赛页面</h5>";
				echo "<h5>第一次登记完成请先<a href=\"http://acm.hust.edu.cn/JudgeOnline/logout.php\">登出</a>一次再重新登录（点完按刷新）</h5>";
				exit();
			}
		} else {
		?>
			<h3>请不要使用非法手段提交错误的数据，谢谢合作！</h3>
		<?php } ?>
		</center>
	<?php }
	$sid = $_SESSION['user_id'];
	if (!isset($_SESSION['user_id'])){ ?>
		<center>
			<h3><a href="http://acm.hust.edu.cn/JudgeOnline/loginpage.php">Please Login First!</a></h3>
			<h3>如果你已经登录了，依然看到了本页面，请按F5刷新一下。</h3>
		</center>
	<?php } else { 
	$sql = "SELECT `ojid`,`name`,`stdnum`,`id`,`school`,`email` FROM `alibaba` WHERE `ojid`='$sid'";
	$result=mysql_query($sql);
	$rows_cnt=mysql_num_rows($result);
	if ($rows_cnt>=1){
		$row=mysql_fetch_object($result); ?>
		<center>
			<h3>本ID已经注册登记过，下面是您注册的信息</h3>
			<table style="text-align:left">
				<tr> <td>Online Judge ID: </td> <td><?php echo $sid; ?> (<a href="http://acm.hust.edu.cn/JudgeOnline/logout.php">换一个ID登录？</a>)</td></tr>
				<tr> <td>姓名: </td> <td> <?php echo $row->name; ?></td></tr>
				<tr> <td>学生证号: </td> <td> <?php echo $row->stdnum; ?></td></tr>
				<tr> <td>身份证号: </td> <td> <?php echo $row->id; ?></td></tr>
				<tr> <td>学校:  </td><td><? echo ($row->school)?'华中科技大学':'其他学校'; ?></td>
				<tr> <td>Email: </td><td><? echo $row->email; ?></td> </tr>
			</table>
			<h4>第一次登记完成请先<a href="http://acm.hust.edu.cn/JudgeOnline/logout.php">登出</a>一次再重新登录（点完按刷新）</h4>
			<h4><a href="http://acm.hust.edu.cn/thx/contest.php?cid=1079">点这里跳转到比赛</a></h4>
			<h4>请保证您的信息填写正确并与在阿里巴巴官网报名信息保持一致，提交后将无法修改，如需特别修改请在<a href="http://acm.hust.edu.cn/thx/discuss.php">这里</a>发帖说明 </h4>
		</center>
	<?php } else {?>
	<center>
		<form action="alibaba.php" method="post">
		<table style="text-align:left">
			<tr> <td>Online Judge ID: </td> <td><?php echo $sid; ?> (<a href="http://acm.hust.edu.cn/JudgeOnline/logout.php">换一个ID登录？</a>)</td></tr>
			<tr> <td>姓名: </td> <td> <input type="text" name="name"></td></tr>
			<tr> <td>学生证号: </td> <td> <input type="text" name="stdnum"> (*请保证唯一性)</td></tr>
			<tr> <td>身份证号: </td> <td> <input type="text" name="pid"> (*请输入您的正确的身份证号)</td></tr>
			<tr> <td>学校:  </td><td> <input type="radio" name="ishust" value = "1" checked="checked">华中科技大学 <input type="radio" name="ishust" value = "0"> 其他学校</td>
			<tr> <td>Email: </td><td> <input type="text" name="email"> (*请保证邮箱的唯一性)</td> </tr>
		</table>
		<input type="hidden" value="<?php echo $sid; ?>" name="sid">
		<input type="submit">
		</form>
		<h4>请保证您的信息填写正确并与在阿里巴巴官网报名信息保持一致，提交后将无法修改，如需特别修改请在<a href="http://acm.hust.edu.cn/thx/discuss.php">这里</a>发帖说明 </h4>
	</center>
	<?php } 
	}?>
</body>
</html>
